<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 头像区域 -->
      <div class="avatar-box">
        <img src="@/assets/img/logo1.jpg" alt="" />
      </div>
      <!-- 表单区域 -->

      <el-form class="login-form" ref="loginFromRef" :model="formLogin" :rules="loginRules" hide-required-asterisk>
        <!-- 注意 验证是通过prop属性加在item项里面的 -->
        <el-form-item prop="username" label="用户名">
          <el-input prefix-icon="iconfont icon-icon_user" v-model="formLogin.username"></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input prefix-icon="iconfont icon-3702mima" v-model="formLogin.password" show-password></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" round @click="login">登陆</el-button>
          <el-button type="info" round @click="reset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 底部区域 -->
      <div class="footer">
        <span>©与其渴望关怀，不如一起精彩</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 为表单绑定数据
      formLogin: {
        username: 'admin',
        password: '123456'
      },
      // 表单的验证规则
      loginRules: {
        username: [
          // 失去焦点的时候触发验证
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 重置按钮响应事件
    reset() {
      this.$refs.loginFromRef.resetFields()
    },
    // 登陆按钮响应事件
    login() {
      this.$refs.loginFromRef.validate(async valid => {
        // isSuccess为false 说明表单验证不成功
        if (!valid) return
        // 发起ajax请求
        const { data: res } = await this.$http.post('login', this.formLogin)
        if (res.meta.status !== 200) return this.$message.error('登陆失败,请检查用户名和密码是否正确!')
        this.$message.success('登陆成功')
        // 登陆成功后，服务器会返回一个token值，我们需要将其存储在sessionStorage里面，以后每次请求都要携带这个token
        sessionStorage.setItem('token', res.data.token)
        // 然后我们将它跳转到主页里
        this.$router.push('/home')
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login-container {
  height: 100%;
  background-image: url('~@/assets/img/bg1.jpg');
}
.login-box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  height: 450px;
  width: 450px;
  border-radius: 30px;
  background-color: #fff;
  opacity: 0.8;
  .avatar-box {
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 10px;
    height: 130px;
    width: 130px;
    box-shadow: 0 0 10px #ddd;
    border-radius: 50%;
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.login-form {
  margin-top: 90px;
  padding: 0 30px;
}
.btns {
  margin-top: 22px;
  /deep/ .el-form-item__content {
    display: flex;
    flex-direction: column;
    align-items: center;
    .el-button {
      width: 250px;
      margin: 8px 0;
    }
  }
}
.el-form-item {
  margin-bottom: 10px;
}
.el-form {
  height: 320px;
}
.el-button + .el-button {
  margin-left: 0;
}
.footer {
  display: flex;
  justify-content: center;
  font-size: 14px;
  color: #999aaa;
}
</style>
